class CommentsController < ApplicationController
  def index
    @post = Post.find(params[:post_id])
  end
  
  def show
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @comment }
    end
  end
  
  def new
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build
    
    respond_to do |format|
      format.html
      format.json {render json: @comment}
    end
  end
  
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.build(params[:comment])

    respond_to do |format|
      if @comment.save
        format.html {redirect_to post_comments_path(@post), notice: 'Comment Created'}
        format.json {render json: @comment, status: :created, location: @post}
      else
        format.html {render action: "new"}
        format.json { render json: @post.errors, status: :unprocessable_entity } 
      end
    end
  end
  
  def edit
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id]) 
    respond_to do |format|
      format.html
      format.json {render json: @comment}
    end
  end
  
  def update
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    respond_to do |format|
      if @comment.update_attributes(params[:comment])
        format.html {redirect_to post_comments_path(@post), notice: 'Comment Created'}
        format.json {render json: @comment, status: :created, location: @post}
      else
        format.html {render action: "new"}
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end
end    
